home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / DARECNO.C < prev    next >
C/C++ Source or Header  |  1993-08-11  |  5KB  |  156 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    darecno.c
  5. //   Title:    Data File Build Utility Library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains code to 
  25. //
  26. //    The code in this module should be written entirely in C.
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        OS/2 2.X+
  33. //        OS/2 2.0 PM
  34. //        SCO UNIX.
  35. //
  36. //    The following compilers are supported:
  37. //        MSC 6.0A
  38. //        MSC/C++ 7.0
  39. //        Borland C++ 3.1 for DOS
  40. //        Borland C++ 1.0 for OS/2 2.X
  41. //        SCO UNIX cc
  42. //
  43. //----------------------------------------------------------------------------
  44. #include <da.h>
  45. #include <dio.h>
  46.  
  47. //----------------------------------------------------------------------------
  48. //    This constant is the size of each block. Since this is a two level
  49. //    file (at most), the maximum number of blocks must be 
  50. //        <= BLOCK_SIZE * BLOCK_SIZE
  51. //
  52. //    Current size is 768 which allows for a 589 Mb data file with 1024 byte
  53. //    blocks
  54. //----------------------------------------------------------------------------
  55. #define BLOCK_SIZE    (768)
  56.  
  57.  
  58. //----------------------------------------------------------------------------
  59. //   Description:    Write record count information to data file.
  60. //    Parameters:    
  61. //       Returns:    TRUE if successful.
  62. //----------------------------------------------------------------------------
  63. BOOL FN_E DataRecNo(PDATACFG pcfg)
  64. {
  65.     FLAG16 fs = FL_OPEN|FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
  66.     HF hfRecNo, hf = -1;
  67.     BOOL fResult = FALSE;
  68.     FPOS fsize, fpos;
  69.     SIZET cRecNo;
  70.     LONG aulRecNo[BLOCK_SIZE];                // Record # index buffer
  71.     SIZET cRecNo2 = 0;                        // Level 2
  72.     LONG aulRecNo2[BLOCK_SIZE];            // Level 2 buffer
  73.     USHORT ausRecNo[BLOCK_SIZE];            // Input buffer
  74.     ULONG ulRecords = 0;
  75.     BOOL f2Level = FALSE;
  76.  
  77.     if (pcfg->fVerbose)
  78.         Output("Building record number index.\n");
  79.  
  80.     if (!FileOpen(&hfRecNo, pcfg->szRecNo, fs, NULL))
  81.         return FALSE;
  82.  
  83.     if (pcfg->fVerbose)
  84.         Output("Opened file '%s'\n.\n", pcfg->szRecNo);
  85.  
  86.     fsize = FileGetSize(hfRecNo);
  87.     if (fsize < 0)
  88.         goto ERROR_EXIT;
  89.  
  90.     Assert(fsize > 0 && (fsize % sizeof(USHORT)) == 0);
  91.     fsize /= sizeof(USHORT);
  92.  
  93.     Assert(fsize <= (FPOS)BLOCK_SIZE * (FPOS)BLOCK_SIZE);
  94.  
  95.     if (!FileSetPos(hfRecNo, 0, FL_BEGIN))
  96.         goto ERROR_EXIT;
  97.  
  98.     if (!DioAppend(pcfg->szData, pcfg->szLogical, DFT_ISAM_RECNO,
  99.     sizeof(aulRecNo), &hf, NULL))
  100.         goto ERROR_EXIT;
  101.  
  102.     fpos = FileGetPos(hf);
  103.     memset(aulRecNo2, 0, sizeof(aulRecNo2));
  104.     if (fsize > BLOCK_SIZE)
  105.         {
  106.         if (!FileWrite(hf, aulRecNo2, sizeof(aulRecNo2), fpos))
  107.             goto ERROR_EXIT;
  108.         f2Level = TRUE;
  109.         }
  110.  
  111.     while (fsize)
  112.         {
  113.         SIZET cRead = (SIZET)MIN(fsize, (FPOS)BLOCK_SIZE);
  114.  
  115.         if (!FileRead(hfRecNo, ausRecNo, cRead * sizeof(USHORT), -1))
  116.             goto ERROR_EXIT;
  117.  
  118.         for (cRecNo = 0; cRecNo < cRead; ++cRecNo)
  119.             {
  120.             ulRecords += (ULONG)ausRecNo[cRecNo];
  121.               aulRecNo[cRecNo] = ulRecords;
  122.             }
  123.         for (; cRecNo < BLOCK_SIZE; ++cRecNo)
  124.               aulRecNo[cRecNo] = ulRecords;
  125.  
  126.         aulRecNo2[cRecNo2++] = ulRecords;
  127.         if (!FileWrite(hf, aulRecNo, sizeof(aulRecNo), -1))
  128.             goto ERROR_EXIT;
  129.  
  130.         fsize -= (FPOS)cRead;
  131.         }
  132.     if (f2Level)
  133.         {
  134.         for (; cRecNo2 < BLOCK_SIZE; ++cRecNo2)
  135.             aulRecNo2[cRecNo2] = ulRecords;
  136.  
  137.         if (!FileWrite(hf, aulRecNo2, sizeof(aulRecNo2), fpos))
  138.             goto ERROR_EXIT;
  139.         }
  140.     fResult = TRUE;
  141. ERROR_EXIT:
  142.     if (hf >= 0)                                // Update data file
  143.         if (!DioAppendClose(fResult, TRUE))
  144.             fResult = FALSE;
  145.     if (!FileClose(hfRecNo))
  146.         fResult = FALSE;
  147.     if (fResult && !FnameDelete(pcfg->szRecNo))
  148.         fResult = FALSE;
  149.     if (pcfg->fVerbose)
  150.         Output("Done.\n.\n");
  151.     return fResult;
  152. }
  153. //----------------------------------------------------------------------------
  154. //------------------------------- End of File --------------------------------
  155. //----------------------------------------------------------------------------
  156.